package cn.jingsheng.backup.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.jingsheng.backup.bean.BackupFile;
import cn.jingsheng.backup.bean.PagedListInfo;
import cn.jingsheng.backup.util.DbUtil;

/**
 * 报备系统详细信息数据库操作类
 * 
 * @author 李晓丹
 * 
 */
public class BackupFileDao {
	/**
	 * 日志记录
	 */
	private static Log log = LogFactory.getLog(BackupFileDao.class);

	/**
	 * 插入数据库
	 * 
	 * @param backupFile
	 *            入库对象
	 * @return
	 */
	public int insert(BackupFile backupFile) {
		PreparedStatement ps = DbUtil.prepareStatement("insert into backup_file(ID,ALIAS_FILE_NAME,COMPANY,CREATE_TIME,CREATE_USER,SEARCH_KEY) values(?,?,?,?,?,?)");
		int i = 1;
		try {
			ps.setString(i++, backupFile.getId());
			ps.setString(i++, backupFile.getAliasFileName());
			ps.setString(i++, backupFile.getCompany());
			ps.setTimestamp(i++, backupFile.getCreateTime());
			ps.setString(i++, backupFile.getCreateUser());
			ps.setString(i++, backupFile.getSearchKey());
			return ps.executeUpdate();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
	}

	/**
	 * 删除单条数据
	 * 
	 * @param backupFile
	 *            删除对象
	 * @return
	 */
	public int delete(BackupFile backupFile) {
		PreparedStatement ps = DbUtil.prepareStatement("delete from backup_file where id = ?", backupFile.getId());
		try {
			return ps.executeUpdate();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
	}

	/**
	 * 根据主键ID查询
	 * 
	 * @param id
	 * @return
	 */
	public BackupFile queryById(long id) {
		/** 查询数据库 */
		PreparedStatement ps = DbUtil.prepareStatement("select * from backup_file where id = ?", id);
		BackupFile backup = new BackupFile();
		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
			if (rs.next()) {
				// 字段赋值
				backup.setId(rs.getString("id"));
				backup.setAliasFileName(rs.getString("alias_file_name"));
				backup.setCompany(rs.getString("company"));
				backup.setCreateTime(rs.getTimestamp("create_Time"));
				backup.setCreateUser(rs.getString("create_User"));
				backup.setSearchKey(rs.getString("search_Key"));
				backup.setEditable(rs.getInt("editable") == 1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
		return backup;

	}

	/**
	 * 根据相关字段查询(按字段是否有值判断)
	 * 
	 * @param backupFile
	 *            查询条件
	 * @param info
	 *            分页信息
	 * @return
	 */
	public List<BackupFile> queryList(BackupFile backupFile, PagedListInfo info) {
		// 查询语句
		StringBuilder sql = new StringBuilder("select * from backup_file where 1=1 ");
		// 参数列表
		List<Object> params = new ArrayList<Object>();
		/** 按字段是否有值生成查询语句 */
		if (backupFile.getId() != null) {
			sql.append(" and id=? ");
			params.add(backupFile.getId());
		}
		if (backupFile.getAliasFileName() != null) {
			sql.append(" and alias_file_name=? ");
			params.add(backupFile.getAliasFileName());
		}
		if (backupFile.getCompany() != null) {
			sql.append(" and company=? ");
			params.add(backupFile.getCompany());
		}
		if (backupFile.getCreateTime() != null) {
			sql.append(" and create_time=? ");
			params.add(backupFile.getCreateTime());
		}
		if (backupFile.getCreateUser() != null) {
			sql.append(" and create_user=? ");
			params.add(backupFile.getCreateUser());
		}
		if (backupFile.getFileType() != null) {
			sql.append(" and alias_file_name like '%.");
			sql.append(backupFile.getFileType());
			sql.append("' ");
		}
		if (backupFile.getSearchKey() != null && backupFile.getSearchKey().length() != 0) {
			// 按空格分隔关键字
			String[] keys = backupFile.getSearchKey().split(" ");
			sql.append(" AND ");
			for (int i = 0; keys != null && i != keys.length; i++) {
				sql.append(i == 0 ? " ( " : " or ");
				sql.append(" search_key like ? ");
				params.add("%" + keys[i] + "%");
			}
			sql.append(" ) ");
		}
		/** 处理分页信息 */
		if (info != null) {
			info.process(sql, params.toArray());
		}
		// 查询结果集
		List<BackupFile> list = new ArrayList<BackupFile>();
		/** 查询数据库 */
		PreparedStatement ps = DbUtil.prepareStatement(sql.toString(), params.toArray());
		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
			while (rs.next()) {
				// 封装对象
				BackupFile backup = new BackupFile();
				backup.setId(rs.getString("id"));
				backup.setAliasFileName(rs.getString("alias_file_name"));
				backup.setCompany(rs.getString("company"));
				backup.setCreateTime(rs.getTimestamp("create_Time"));
				backup.setCreateUser(rs.getString("create_User"));
				backup.setSearchKey(rs.getString("search_Key"));
				backup.setEditable(rs.getInt("editable") == 1);
				list.add(backup);
			}
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(rs);
			DbUtil.close(ps);
		}
		return list;
	}

	/**
	 * 更新指定对象
	 * 
	 * @param backupFile
	 * @return
	 */
	public int updateById(BackupFile backupFile) {

		/** 查询数据库 */
		PreparedStatement ps = DbUtil.prepareStatement("update backup_file set editable=? where id = ?", backupFile.isEditable() ? 1 : 0, backupFile.getId());
		try {
			return ps.executeUpdate();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		} finally {
			DbUtil.close(ps);
		}
	}
}
